package com.of.controller;

import com.of.service.FileService;

import com.of.vo.ImageVo;
import com.of.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Api("/file")
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")//alt+insert自动生成
    @PostMapping("/upload") //MultipartFile底层实现的就是常规IO流，简化用户的操作过程，无需手动关流
    public SysResult fileUpload(MultipartFile file) {
        ImageVo imageVo = fileService.upload(file);
        if (imageVo == null) {
            return SysResult.fail();
        }
        return SysResult.success(imageVo);
    }

    @ApiOperation(value = "删除文件", notes = "删除文件", httpMethod = "DELETE")
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }

}

